package projetolp2.GraphicInterface;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

import projetolp2.Sistem.Estabelecimento;
import projetolp2.Sistem.Sistema;

public class SugestoesUsuario extends MealPoints implements ActionListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JPanel panelMenu = new JPanel(new BorderLayout());
	private JPanel panel1 = new JPanel(new GridLayout(0, 1));
	private JPanel panel2 = new JPanel(new GridLayout(0, 1));
	private JPanel panel3 = new JPanel(new GridLayout(0, 1));
	private JPanel panel0 = new JPanel(new GridLayout(0, 1));
	private JPanel panelFinalbotoes = new JPanel(new GridLayout(0, 1));
	private JButton VOLTAR = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getVoltar());
	private JButton VOLTAR2 = new JButton(NomeBotoes.values()[idioma(IDIOMAPROGRAMA )].getVoltar());
	private JButton BUSCAR = new JButton("Buscar");
	private JLabel label;
	private int quantidade = 0;
	private JComboBox USUARIOS = new JComboBox();
	private JPanel panelFinal = new JPanel();
	private ButtonGroup bg = new ButtonGroup();
	private ButtonGroup bg2 = new ButtonGroup();
	private JPanel panelbgfinal = new JPanel(new GridLayout(0, 1));
	private JPanel panel = new JPanel();
	private JPanel panelbg2 = new JPanel();
	private JRadioButton afalbetica = new JRadioButton("Alfabética");
	private JRadioButton tipo = new JRadioButton("Aleatório");
	private JRadioButton cantina = new JRadioButton("A la carte");
	private JRadioButton bar = new JRadioButton("Prato feito");
	private JRadioButton restaurante = new JRadioButton("Self service");
	public SugestoesUsuario(){
		
		bg.add(afalbetica);
		bg.add(tipo);

		panel = new JPanel();
		panel.add(afalbetica);
		panel.add(tipo);
		
		bg2.add(cantina);
		bg2.add(bar);
		bg2.add(restaurante);

		panelbg2 = new JPanel();
		panelbg2.add(cantina);
		panelbg2.add(bar);
		panelbg2.add(restaurante);
		
		panelbgfinal.add(panel);
        panelbgfinal.add(panelbg2);

        panelFinalbotoes.add(panel2);
        panelFinalbotoes.add(panel3);
	}private void ajudaDeBotoes(){
		VOLTAR.setToolTipText("Voltar a janela anterior");
	}
	
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == VOLTAR){
			removerAdicionar(((SugestoesUsuario) sugestoesUsuario).panel(),sugestoespersonalizadas );
		}
		else if (e.getSource() == BUSCAR){
			removerAdicionar(((SugestoesUsuario) sugestoesUsuario).panel(), resultConsulta());
		}
		else if (e.getSource() == VOLTAR2){
			janela.enable();
			
		}
	}
	@SuppressWarnings("serial")
	public JPanel resultConsulta(){
		panelFinal.removeAll();
		panelMenu.removeAll();
		panel0.removeAll();
		label = new JLabel("Os "+quantidade+" melhores restaurantes: ");
		
        List<Estabelecimento> recomendacao;
		JList texto = new JList();
		List<String> saida;
		try {
			if(cantina.isSelected()){
				recomendacao = SISTEMA.recomendacaoUsuarioAlmoco(USUARIOS.getSelectedItem().toString(),
						quantidade, "a la carte");
				saida = Sistema.converteEstabelecimentos(recomendacao);
			}
			else if(bar.isSelected()){
				recomendacao = SISTEMA.recomendacaoUsuarioAlmoco(USUARIOS.getSelectedItem().toString(),
						quantidade, "prato-feito");
				saida = Sistema.converteEstabelecimentos(recomendacao);
			}
			else if(restaurante.isSelected()){
				recomendacao = SISTEMA.recomendacaoUsuarioAlmoco(USUARIOS.getSelectedItem().toString(),
						quantidade, "self-service");
				saida = Sistema.converteEstabelecimentos(recomendacao);
			}
			else{
				recomendacao = SISTEMA.recomendacaoPorUsuario(USUARIOS.getSelectedItem().toString(),quantidade);
				saida = Sistema.converteEstabelecimentos(recomendacao);
			}
	        String estabelecimentos = "Nome            -           Endere�o;;;";
	        if(afalbetica.isSelected()){
				Collections.sort(saida);
			}
			for (String i : saida){
				 estabelecimentos += i+";------------------------------------------------------------------;";
			 }
			try{
	        	final String valorees = estabelecimentos;
	        	//coloca os valores na lista que ira ser mostrada
	        texto.setModel(new AbstractListModel() {
	        	String[] valores = valorees.split(";");
	            public int getSize() { return valores.length; }
	            public Object getElementAt(int i) { return valores[i]; }
	        });
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		

        texto.setName("jList1"); // NOI18N
		}catch(Exception e){
        	System.err.println(e.getMessage());
        }

		panel0.add(new JScrollPane(texto));
		
        panelFinal.add(panelbgfinal);
        panelFinal.add(panel1);
        panelFinal.add(panelFinalbotoes);
        panelMenu.add(panelFinal, BorderLayout.NORTH);
        panelMenu.add(panel0);
        
        return  panelMenu;
	}
	public void quantidade(int valor){
		quantidade = valor;
	}
	@SuppressWarnings("serial")
	private void createComponents() {
		ajudaDeBotoes();
		USUARIOS.removeAllItems();
		 try {
				List<String> listaUsuarios = SISTEMA.nomeDeUsuarios();
				for (int i = 0; i < listaUsuarios.size();i++){
		        	USUARIOS.addItem(listaUsuarios.get(i));
		        }
			} catch (Exception e) {
				e.printStackTrace();
			}
		

        
		label = new JLabel("Os "+quantidade+" melhores restaurantes: ");
		
		panel1.removeAll();
		panel2.removeAll();
		panelMenu.removeAll();
		panel0.removeAll();
		ajudaDeBotoes();
		
		String sugestoes = "";
		label = new JLabel("Os "+quantidade+" melhores restaurantes: ");
		for(int i = 1;i<=quantidade;i++){
			sugestoes += (i+" - ESTABELECIMENTO     -     endere�o;---------------------------------------------------------------------------------------;");
		}
		JList texto = new JList();
		
		try{
        	final String valorees = sugestoes;
        	//coloca os valores na lista que ira ser mostrada
        texto.setModel(new AbstractListModel() {
        	String[] valores = valorees.split(";");
            public int getSize() { return valores.length; }
            public Object getElementAt(int i) { return valores[i]; }
        });

        texto.setName("jList1"); // NOI18N
		}catch(Exception e){
        	System.err.println(e.getMessage());
        }

		panel0.add(new JScrollPane(texto));
		
		panel0.setBorder(BorderFactory.createEmptyBorder(
                10, //top
                10, //left
                10, //bottom
                10) //right
                );
		
        VOLTAR.setMnemonic(KeyEvent.VK_I);
        VOLTAR.addActionListener(this);
        label.setLabelFor(VOLTAR);
        
        VOLTAR2.setMnemonic(KeyEvent.VK_I);
        VOLTAR2.addActionListener(this);
        label.setLabelFor(VOLTAR2);
        
        BUSCAR.setMnemonic(KeyEvent.VK_I);
        BUSCAR.addActionListener(this);
        label.setLabelFor(BUSCAR);
        
        USUARIOS.setBackground(Color.WHITE);
        panel2.add(BUSCAR);
        panel3.add(VOLTAR);
        panel1.add(USUARIOS);
        panel1.setBorder(BorderFactory.createEmptyBorder(
                10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel2.setBorder(BorderFactory.createEmptyBorder(
                10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panel3.setBorder(BorderFactory.createEmptyBorder(
                10, //top
                10, //left
                10, //bottom
                10) //right
                );
        panelFinal.add(panelbgfinal);
        panelFinal.add(panel1);
        panelFinal.add(panelFinalbotoes);
        panelMenu.add(panelFinal, BorderLayout.NORTH);
        panelMenu.add(panel0);
    }
	public Component panel(){
		createComponents();
		panelMenu.setVisible(true);
		return panelMenu;
	}
}


